local lspconfig = require('lspconfig')
local capabilities = require('cmp_nvim_lsp').default_capabilities()
-- capabilities.offsetEncoding = { "utf-16" };

require("mason").setup()
require("mason-lspconfig").setup()

lspconfig.lua_ls.setup {
    capabilities = capabilities,
}

lspconfig.clangd.setup {
    capabilities = vim.tbl_deep_extend('force', capabilities, {
        offsetEncoding = { 'utf-8' },
        general = {
            positionEncodings = { 'utf-8' },
        },
    }),
}

-- Global mappings.
-- See `:help vim.diagnostic.*` for documentation on any of the below functions
vim.keymap.set('n', '<leader>e', vim.diagnostic.open_float)
-- 跳到上一个诊断并显示浮窗
vim.keymap.set('n', '[d', function()
  if vim.diagnostic.jump then
    -- Neovim 0.11+ 新 API
    vim.diagnostic.jump({ count = -1, float = true })
  else
    -- Neovim 0.10 及以前
    vim.diagnostic.goto_prev({ float = true })
  end
end, { desc = 'Prev diagnostic' })

-- 跳到下一个诊断并显示浮窗
vim.keymap.set('n', ']d', function()
  if vim.diagnostic.jump then
    vim.diagnostic.jump({ count = 1, float = true })
  else
    vim.diagnostic.goto_next({ float = true })
  end
end, { desc = 'Next diagnostic' })
vim.keymap.set('n', '<leader>q', vim.diagnostic.setloclist)

-- Use LspAttach autocommand to only map the following keys
-- after the language server attaches to the current buffer
vim.api.nvim_create_autocmd('LspAttach', {
    group = vim.api.nvim_create_augroup('UserLspConfig', {}),
    callback = function(ev)
        -- Enable completion triggered by <c-x><c-o>
        vim.bo[ev.buf].omnifunc = 'v:lua.vim.lsp.omnifunc'

        -- Buffer local mappings.
        -- See `:help vim.lsp.*` for documentation on any of the below functions
        local opts = { buffer = ev.buf }
        vim.keymap.set('n', 'gD', vim.lsp.buf.declaration, opts)
        -- vim.keymap.set('n', 'gd', vim.lsp.buf.definition, opts)
        vim.keymap.set('n', 'K', vim.lsp.buf.hover, opts)
        -- vim.keymap.set('n', 'gi', vim.lsp.buf.implementation, opts)
        vim.keymap.set('n', '<C-k>', vim.lsp.buf.signature_help, opts)
        vim.keymap.set('n', '<leader>wa', vim.lsp.buf.add_workspace_folder, opts)
        vim.keymap.set('n', '<leader>wr', vim.lsp.buf.remove_workspace_folder, opts)
        vim.keymap.set('n', '<leader>wl', function()
            print(vim.inspect(vim.lsp.buf.list_workspace_folders()))
        end, opts)
        vim.keymap.set('n', '<leader>D', vim.lsp.buf.type_definition, opts)
        vim.keymap.set('n', '<leader>rn', vim.lsp.buf.rename, opts)
        vim.keymap.set({ 'n', 'v' }, '<leader>ca', vim.lsp.buf.code_action, opts)
        -- vim.keymap.set('n', 'gr', vim.lsp.buf.references, opts)
        vim.keymap.set('n', '<leader>fa', function()
            vim.lsp.buf.format { async = true }
        end, opts)

        -- 需要用 telescope 替代的映射
        local has_telescope, telescope = pcall(require, 'telescope.builtin')
        opts = { noremap = true, silent = true }
        if has_telescope then
          vim.keymap.set('n', 'gd', telescope.lsp_definitions, opts)
          vim.keymap.set('n', 'gi', telescope.lsp_implementations, opts)
          vim.keymap.set('n', 'gr', telescope.lsp_references, opts)
          -- vim.keymap.set('n', 'gD', telescope.lsp_declarations, opts)
        else
          -- fallback 到默认 LSP 方法
          vim.keymap.set('n', 'gd', vim.lsp.buf.definition, opts)
          vim.keymap.set('n', 'gi', vim.lsp.buf.implementation, opts)
          vim.keymap.set('n', 'gr', vim.lsp.buf.references, opts)
        end

    end,
})
